package com.service.config.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName PropertiesUtils
 * @Description TODO
 * @Author mamingcong
 * @Date 2020/6/4 15:00
 * @Version 1.0
 */
public class PropertiesUtils {
    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

    /**
     * 读取json文件，返回json字符串
     * @param fileName
     * @return
     */
    public static String readJsonFile(String fileName) throws FileNotFoundException {
        String content = "";
        try {
            ClassPathResource classPathResource = new ClassPathResource(fileName);
            InputStreamReader inputStreamReader = new InputStreamReader(classPathResource.getInputStream(), StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuffer sb = new StringBuffer();
            while ((content = bufferedReader.readLine()) != null) {
                sb.append(content);
            }
            String jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("读取文件报错", e);
        }
        return null;
    }
}
